<template>
  <a-modal
    :maskClosable="false"
    :visible="visible"
    :width="1000"
    @cancel="handleCancel"
    title="导入向导"
  >
    <div class="self-box">
      <div class="self-box-item">
        <div @click="download" class="upload-dragger">
          <a-upload-dragger
            :headers="headers"
            :multiple="true"
            :openFileDialogOnClick="false"
            @change="handleChange"
            action="/api/storage/upload"
            name="file"
          >
            <p class="ant-upload-drag-icon">
              <a-icon type="cloud-download"/>
            </p>
            <p class="ant-upload-text">先下载要上传的模板</p>
            <p class="ant-upload-hint">
              支持扩展名：xls
            </p>
          </a-upload-dragger>
        </div>
      </div>
      <div class="self-box-item">
        <div
          class="upload-dragger"
        >
          <a-upload-dragger
            :beforeUpload="beforeFileUpload"
            :fileList="fileList"
            :headers="headers"
            :multiple="true"
            @change="handleChange"
            :action="baseUrl + actionUrl"
            name="file"
          >
            <p class="ant-upload-drag-icon">
              <a-icon type="cloud-upload"/>
            </p>
            <p class="ant-upload-text">上传要导入的文件</p>
            <p class="ant-upload-hint">
              支持扩展名：xls
            </p>
          </a-upload-dragger>
        </div>
      </div>
    </div>

    <a-divider/>
    <div class="step-form-style-desc">
      <p v-html="des" />
    </div>
    <template slot="footer">
      <a-button @click="handleCancel" key="cancel">取消</a-button>
    </template>
  </a-modal>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'BImportFile',
  props: {
    actionUrl: {
      type: String,
      required: true
    },
    des: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      headers: {
        'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)
      },
      baseUrl: process.env.VUE_APP_API_BASE_URL,
      visible: false,
      fileList: [],
      fileTypeList: ['application/vnd.ms-excel']
    }
  },

  methods: {
    show () {
      this.visible = true
    },
    handleCancel () {
      this.fileList = []
      this.visible = false
    },
    beforeFileUpload (file) {
      return new Promise((resolve, reject) => {
        // const index = this.fileTypeList.indexOf(file.type)
        // if (index < 0) {
        //   this.$message.error(`您只能上传 .xls 文件`)
        //   return reject(new Error(`您只能上传 .xls 文件`))
        // }
        return resolve(true)
      })
    },
    download () {
      this.$emit('download')
    },
    handleChange (info) {
      this.fileList = [...info.fileList]
      if (info.file.status === 'done') {
        const res = info.file.response
        if (res.code === 0) {
          this.$message.success('数据导入成功')
          setTimeout(() => {
            this.visible = false
            this.fileList = []
            this.$emit('ok')
          }, 1000)
        } else {
          this.$message.error(res.msg, 10)
        }
      } else if (info.file.status === 'error') {
        this.$message.error('数据导入失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.self-box {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 20px 70px;
}

.self-box-item {
  width: 360px;
  height: 140px;
}

.step-form-style-desc {
  padding: 0 56px;
  color: rgba(0, 0, 0, .45);

  h3 {
    margin: 0 0 12px;
    color: rgba(0, 0, 0, .45);
    font-size: 16px;
    line-height: 32px;
  }

  h4 {
    margin: 0 0 4px;
    color: rgba(0, 0, 0, .45);
    font-size: 14px;
    line-height: 22px;
  }

  p {
    margin-top: 0;
    margin-bottom: 12px;
    line-height: 22px;
  }
}
</style>
